<?php
namespace app\api\controller\v1;

use app\logic\AttachmentLogic;
use app\api\controller\BaseController;
use app\exception\FileIsExistsException;
// use app\model\Attachment;
class Attachment extends BaseController
{
    protected $name = '附件';

    protected $methodName = [
        'upload' => '客户端上传附件',
    ];

    /**
     * @Description: 客户端上传图片
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-08 10:58:27
     * @version: 1.0.0
     * @return [type]
     */
    public function upload()
    {
        $params = $this->request->post();
        $attachmentLogic = new AttachmentLogic();
        $result = $attachmentLogic->upload();

        if ($result) {
            unset($result[0]['savename'], $result[0]['path']);
            $params = array_merge($params, $result[0]);
        }
        $model = $attachmentLogic->getModel();

        $detail = $model->where(['admin_id' => $params['admin_id'], 'sha1' => $params['sha1']])->find();

        if ($detail) {
            $this->formatModel($detail);
            throw new FileIsExistsException(['data' => ['detail' => $detail]]);
        }
        $detail = $model->create($params);

        return success(['detail'=>$detail],'获取成功');
        // print_r($detail);
        // die;
    }
}
